$(document).ready(function () {

    $('#add_user').on("click", function () {
        $.ajax({
            url: "/rbac/user/add/",
            type: 'POST',
            data: {
                'username': $("#myModal #id_username").val(),
                'passwd': $("#myModal #id_passwd").val(),
                're_passwd': $("#myModal #id_re_passwd").val(),
                'email': $("#myModal #id_email").val(),
                'phone': $("#myModal #id_phone").val(),
                'fullname': $("#myModal #id_fullname").val(),
                'csrfmiddlewaretoken': $('[name="csrfmiddlewaretoken"]').val(),
            },
            success: function (data) {
                if (data['status']) {
                    window.location.reload()
                } else {
                    delete data.status;
                    $(".span-errors").text(null);
                    for (var key in data) {
                        $(`#id_${key}`).next().eq(0).text(data[key][0]);
                    }
                }
            },
            error: function (data) {
                console.log(data)
            }
        });
    });
    $('#edit_user').click(function () {
        var pk = $(this).attr('data-id');
        $.ajax({
            url: `/rbac/user/edit/${pk}/`,
            type: 'POST',
            data: {
                'fullname': $("#myModal1 #id_up_fullname").val(),
                'emai': $("#myModal1 #id_up_emai").val(),
                'phone': $("#myModal1 #id_up_phone").val(),
                'csrfmiddlewaretoken': $('[name="csrfmiddlewaretoken"]').val(),
            },
            success: function (data) {
                if (data['status']) {
                    window.location.reload()
                } else {
                    delete data.status;
                    $(".span-errors").text(null);
                    for (var key in data) {
                        $(`#id_up_${key}`).next().eq(0).text(data[key][0]);
                    }
                }
            },
        })
    });
    $('.get_user_info').click(function () {
        var pk = $(this).attr('data-id');
        $.ajax({
            url: `/rbac/user/edit/${pk}`,
            type: 'GET',
            success: function (data) {
                if (data.status) {
                    delete data.status;
                    for (var key in data) {
                        $(`#id_up_${key}`).val(data[key]);
                    }
                    $("#edit_user").attr('data-id', pk)
                } else {
                    $('div').remove('.modal-backdrop');
                    $("#myModal1").removeClass('show').css('display', 'none');
                    //$("#myModal1").css('display','none');
                    swal({
                        title: "警告",
                        text: data.errors,
                        type: "warning",
                    });
                }
            },

        })
    });

});